from sys import argv 
from xml.dom import minidom 
 
class XmlToWiki:
	 
	def __init__(self):
		self.domobject = minidom.parse(argv[1])
		self.f = open("wiki.file", "w")
	 
	def getText(self, nodelist):
		rc = ""
		for node in nodelist:
        		if node.nodeType == node.TEXT_NODE:
            			rc = rc + node.data
    		return rc 
	
	def handletitle(self, title, sect):
		entry= self.getText(title)
		if sect == 1:
			entry = "=="+entry+"=="
			self.f.write(entry)
			self.f.write("\n")
		if sect == 2:
			entry = "==="+entry+"==="
			self.f.write(entry)
			self.f.write("\n")
			
	def choosefunction(self, s):
		if s == "title": return self.handletitle
		if s == "para": return self.handlepara
		if s == "sect1": return self.handlesect1
		if s == "sect2": return self.handlesect2
			
	def handlepara(self, paraentry, sect):
		entry= self.getText(paraentry)
		self.f.write(entry)
			
	def handlesect2(self, sect2childnodes, sect):
		for entry in sect2childnodes:
			if entry.nodeType == entry.TEXT_NODE: continue
			else:
				param=entry.childNodes
				function = self.choosefunction(entry.tagName)
				print function, param
				function(param,2)
	 
	def handlesect1(self,sect1lst):
		
		for entry in sect1lst:
			entrychildnodes=entry.childNodes
			for x in entrychildnodes:
				if x.nodeType == x.TEXT_NODE: continue
				param = x.childNodes
				print x
				function = self.choosefunction(x.tagName)
				function(param, 1)
		self.f.close()
					
	def getsect1(self):
		sect1=self.domobject.getElementsByTagName("sect1")
		return sect1


if __name__ == "__main__":
	
	c=XmlToWiki()
	c.handlesect1(c.getsect1())
	
